package cn.lg.soar.system.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.general.model.ConfigDTO;
import cn.lg.soar.system.general.service.IUserConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户配置
 * @author luguoxiang
 * @since 2023-07-13
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "用户配置")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/user-config")
public class UserConfigController {

    @Autowired
    private IUserConfigService service;

    @Log(value = "设置", data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @PostMapping("set")
    public void set(@RequestBody @Validated ConfigDTO data) {
        service.set(UserContext.getUserId(), data.getCode(), data.getValue());
    }

    @GetMapping("get")
    public HttpResult<String> get(@RequestParam String code) {
        return HttpResult.data(service.get(UserContext.getUserId(), code));
    }

    @GetMapping("map")
    public Map<String, String> map(String[] codes){
        return service.map(UserContext.getUserId(), codes);
    }

}

